import request from '../utils/request'

// 用户注册
export function register(data) {
  return request({
    url: '/user/register',
    method: 'post',
    data: {
      name: data.name,
      email: data.email,
      password: data.password
    }
  })
}

// 用户登录
export function login(data) {
  return request({
    url: '/user/login',
    method: 'post',
    data: {
      email: data.email,
      password: data.password
    }
  })
}

// 获取用户信息
export function getUserInfo() {
  return request({
    url: '/user/info',
    method: 'get'
  }).then(response => {
    console.log('getUserInfo API Success Response:', response)
    return response
  }).catch(error => {
    console.error('getUserInfo API Error Response:', error)
    throw error
  })
}

// 更新用户信息
export function updateUserInfo(data) {
  return request({
    url: '/user/update',
    method: 'post',
    data
  })
}

// 检查登录状态
export function checkLogin() {
  return request({
    url: '/user/check-login',
    method: 'get'
  })
}

// 用户发表评论
export function postComment(data) {
  return request({
    url: '/user/comment',
    method: 'post',
    data: {
      content: data.content,
      chapterID: data.chapterID,
      parentCommentID: data.parentCommentID || null // 允许为空
    }
  }).then(response => {
    console.log('postComment API Success Response:', response)
    return response
  }).catch(error => {
    console.error('postComment API Error Response:', error)
    throw error
  })
}

// 获取VIP商品列表
export function getVIPProductList() {
  return request({
    url: '/product/vip/list',
    method: 'get'
  }).then(response => {
    console.log('getVIPProductList API Success Response:', response);
    return response;
  }).catch(error => {
    console.error('getVIPProductList API Error Response:', error);
    throw error;
  });
}

// 购买商品
export function buyProduct(productID) {
  return request({
    url: `/product/pay/${productID}`,
    method: 'get' // 根据接口文档，购买商品是GET请求
  }).then(response => {
    console.log('buyProduct API Success Response:', response);
    return response;
  }).catch(error => {
    console.error('buyProduct API Error Response:', error);
    // 如果是认证错误，给出更友好的提示
    if (error.message && error.message.includes('未登录')) {
      throw new Error('请先登录后再进行购买');
    }
    throw error;
  });
}

// 获取我的订单列表
export function getMyOrders(params = {}) {
  // 模拟订单数据
  const mockOrders = [
    { orderID: '20230520-001', orderDate: '2023-05-20 10:30:00', productName: 'Vue 3 入门与实战', amount: 99.00, status: '已支付' },
    { orderID: '2023-05-19-002', orderDate: '2023-05-19 15:00:00', productName: '年度VIP会员', amount: 299.00, status: '已支付' },
    { orderID: '2023-05-18-003', orderDate: '2023-05-18 20:00:00', productName: 'React 全家桶详解', amount: 129.00, status: '已取消' },
    { orderID: '2023-05-17-004', orderDate: '2023-05-17 12:00:00', productName: 'Node.js 从零到一', amount: 89.00, status: '已支付' },
  ];

  return Promise.resolve({
    code: 1,
    message: '获取成功',
    data: {
      items: mockOrders,
      total: mockOrders.length,
      page: params.page || 1,
      pageSize: params.pageSize || 10
    }
  });
} 